﻿using Api.Application.Command.Role;
using Api.DTO;
using AutoMapper;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.Role
{
    public class DelRoleHandler : IRequestHandler<DelRoleCommand, ApiResult<int>>
    {
        private readonly IBase<RoleModel> roleRepo;
        private readonly IMapper mapper;

        public DelRoleHandler(IBase<RoleModel> roleRepo,IMapper mapper)
        {
            this.roleRepo = roleRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(DelRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var role = roleRepo.GetAll().Where(x => x.Id == request.Id).FirstOrDefault();
            role.IsDeleted = true;
            var r=roleRepo.Update(role).Result;
            if(r>0)
            {
                result.Msg = "删除角色成功";
                result.Code = ApiEnum.OK;
                result.Data = r;
                return Task.FromResult(result);
            }
            else
            {
                result.Msg = "删除角色失败";
                result.Code = ApiEnum.Fail;
                result.Data = r;
                return Task.FromResult(result);
            }
        }
    }
}
